package com.wydexcel.generate.process;

import com.wydexcel.generate.properties.ExcelPositionProperties;
import com.wydexcel.generate.properties.s.ExcelCellBaseProperties;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

public class WydExcelCellMergeProcessImpl implements WydExcelCellMergeProcess {

    @Override
    public void mergeProcess(ExcelPositionProperties merges, ExcelCellBaseProperties baseProperties, Sheet sheet, boolean isBody) {
        try {
            mergeCell(merges.getCellRangeAddress(), sheet, baseProperties);
        } catch (Exception e) {
            return;
        }

    }

    public void mergeCell(CellRangeAddress cellAddresses, Sheet sheet, ExcelCellBaseProperties baseProperties) {
        if (null != baseProperties) {
            RegionUtil.setBorderBottom(baseProperties.getBorderBottom(), cellAddresses, sheet);
            RegionUtil.setBorderLeft(baseProperties.getBorderLeft(), cellAddresses, sheet);
            RegionUtil.setBorderRight(baseProperties.getBorderRight(), cellAddresses, sheet);
            RegionUtil.setBorderTop(baseProperties.getBorderTop(), cellAddresses, sheet);
            //
            if (null != baseProperties.getBottomBorderColor()) {
                RegionUtil.setBottomBorderColor(baseProperties.getBottomBorderColor(), cellAddresses, sheet);
            }
            if (null != baseProperties.getLeftBorderColor()) {
                RegionUtil.setLeftBorderColor(baseProperties.getLeftBorderColor(), cellAddresses, sheet);
            }
            if (null != baseProperties.getRightBorderColor()) {
                RegionUtil.setRightBorderColor(baseProperties.getRightBorderColor(), cellAddresses, sheet);
            }
            if (null != baseProperties.getTopBorderColor()) {
                RegionUtil.setTopBorderColor(baseProperties.getTopBorderColor(), cellAddresses, sheet);
            }
        }
        if (null != cellAddresses) {
            sheet.addMergedRegion(cellAddresses);
        }

    }
}
